#!/usr/bin/env python
# -*- coding: utf-8 -*-

import os
import struct
import numpy as np
import tensorflow as tf


def load_minst(path, kind='train'):
    print("Load MNIST data from %s" % path)

    labels_path = os.path.join(path, '%s-labels-idx1-ubyte' % kind)
    with open(labels_path, 'rb') as lbpath:
        magic, n = struct.unpack('>II', lbpath.read(8))
        labels = np.fromfile(lbpath, dtype=np.uint8)

    images_path = os.path.join(path, '%s-images-idx3-ubyte' % kind)
    with open(images_path, 'rb') as imgpath:
        magic, num, rows, cols = struct.unpack('>IIII', imgpath.read(16))
        images = np.fromfile(imgpath, dtype=np.uint8).reshape(len(labels), 784)

    # return labels
    # return images
    return images, labels


if __name__ == '__main__':
    out = load_minst("/Users/meijian-wyl/work/mnist")
    image = out[0][0]
    label = out[1][0]
    print(label)
    print(image[0])
    print(image[1])


